package cm.stu.dao;

import cm.stu.pojo.Person;
import cm.stu.pojo.StudentAnswer;

import java.util.List;

public class StudentDaoImpl implements StudentDao {
    @Override
    public List<Person> getAllTeacher() {
        //查询老师的信息
        String sql = "select * from person where userIdentify=1";
        return Deal.getAllPerson(sql);

    }

    @Override
    public List<Person> getSearchTeacher(String matchText) {
        //搜索老师查询
//        String sql = "SELECT * FROM person WHERE userAccount=1 AND userAccount = '"+matchText+"' OR userName LIKE '%"+matchText+"%' OR userOtherName LIKE '%"+matchText+"%'";
        String sql = "SELECT * FROM person WHERE userIdentify=1 AND (userAccount = '"+matchText+"' OR userName LIKE '%"+matchText+"%' OR userOtherName LIKE '%"+matchText+"%')";
        return Deal.getAllPerson(sql);
    }

    @Override
    public List<Person> getMyTeacher(String userAccount) {
        //通过学生查询老师
        String sql = "SELECT * FROM person WHERE userAccount IN(SELECT teacherAccount AS userAccount FROM userteacher ut WHERE ut.userAccount = '"+userAccount+"')";
        return Deal.getAllPerson(sql);
    }

    @Override
    public void addTeacher(String userAccount, String teacherAccount) {
        //通过学生账号和老师账号，把"选择老师"插入到"我的老师"功能中
        String sql = "insert into userteacher values ('"+userAccount+"','"+teacherAccount+"')";
        Deal.deal(sql);
    }

    @Override
    public List<StudentAnswer> goMyTeaTask(String userAccount, String teacherAccount) {
        String sql = "SELECT s.*, p.userName, taskName FROM studentTask s INNER JOIN person p ON s.studentAccount = p.userAccount INNER join task t ON s.taskAccount = t.taskAccount WHERE (s.studentAccount = '"+userAccount+"' AND t.teacherAccount = '"+teacherAccount+"')";
        return Deal.getTaskDetail(sql);
    }

    @Override
    public void deleteMyTeacher(String userAccount, String teacherAccount) {
        String sql = "DELETE FROM userteacher WHERE userAccount = '"+userAccount+"' AND teacherAccount = '"+teacherAccount+"'";
        Deal.deal(sql);
        sql = "DELETE FROM studentTask WHERE taskAccount IN(SELECT taskAccount FROM task WHERE teacherAccount = '"+teacherAccount+"') AND studentAccount = '"+userAccount+"'";
        Deal.deal(sql);
    }

    @Override
    public void doAnswer(String userAccount, String taskAccount, String answer) {
        String sql = "UPDATE studentTask SET studentAnswer = '"+answer+"',isFinish = 1 WHERE taskAccount = '"+taskAccount+"' AND studentAccount = '"+userAccount+"'";
        Deal.deal(sql);
    }

    @Override
    public void dealFenshu(String taskAccount, String studentAccount, String fenshu) {
        //taskAccount,studentAccount
        //UPDATE studentTask SET isScore = '90',isFinish = 2 WHERE taskAccount='210116391' AND studentAccount='2022080326';
        String sql = "UPDATE studentTask SET isScore = '"+fenshu+"',isFinish =2 WHERE taskAccount = '"+taskAccount+"' AND studentAccount = '"+studentAccount+"'";
        System.out.println("Executing SQL: " + sql);
        Deal.deal(sql);
    }
}
